package com.sc;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

/**
 * RabbitMQ配置类
 *
 * @author CL
 *
 */
@Configuration
public class RabbitMqConfig {


    /**
     * 交换机名称
     */
    public static final String EXCHANGE_NAME = "c3stones.direct";

    /**
     * 路由键
     */
    public static final String ROUNTING_KEY = "test";

    /**
     * 队列名称
     */
    public static final String QUEUE_NAME = "test.queue";

    /**
     * 配置Direct交换机
     *
     * @return
     */
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(EXCHANGE_NAME);
    }

    /**
     * 配置队列
     *
     * @return
     */
    @Bean
    public Queue testQueue() {
        return new Queue(QUEUE_NAME,true,false,false,null);
    }

    /**
     * 将队列与交换机通过路由键绑定
     *
     * @return
     */
    @Bean
    public Binding binding() {
        return BindingBuilder.bind(testQueue()).to(directExchange()).with("");
    }
    //创建初始化RabbitAdmin对象

    @Bean
    public RabbitAdmin createExchangeAndQuene(RabbitTemplate rabbitTemplate) {
        RabbitAdmin rabbitAdmin = new RabbitAdmin(rabbitTemplate.getConnectionFactory());
        rabbitAdmin.setAutoStartup(true);
//        rabbitAdmin.declareExchange(directExchange());
        rabbitAdmin.declareQueue(testQueue());
//        rabbitAdmin.declareBinding(binding());
        return rabbitAdmin;
    }

}